/* 
 * CMPS 258: Programming Languages, 
 *        Assignment 2 - Part3: "Networks" - March 20, 2008.
 * 
 *        Group name: Doomsday
 *        Authors:  Ramsey Nasser (ID# 200600496) 
 *                  Hadi Abu Rislan (ID# 200600426) 
 *                  Kevin Azzam (ID# 200602033)
 */

#include <stdio.h> 
#include <stdlib.h>

/* TODO: 
 *   - should we make a prompt asking for extra details?  
 */

typedef struct _packet {
    int ID; 
    int start; 
    int end;
    int done; // bool
} packet; 

void packet_init(packet list[], int max);

int main () {
    FILE *in, *out, *tmp; 
    
    tmp = fopen("networks.in", "r"); 
    if (tmp == NULL) {
       fprintf(stderr, "Coulnd't find input file. Program Terminating. \n"); 
       system("pause");
       exit(1); 
    }
    int i, j = i = 0;
    
    int iCases = 0; 
    
    while (!feof(tmp)) {
         int n, m = n = 0; // store temporary n and m here. 
         int junk; // store things to skip here
         fscanf(tmp, "%d", &n); 
         if (n == 0)
              break; 
         iCases++; 
         fscanf(tmp, "%d", &m); 
         for (i = 0; i < n; i++) { // skip next line
             fscanf(tmp, "%d", &junk); 
         }
         for (i = 0; i < 3*m; i++) { // skip next m packets (3 numbers per line)
             fscanf(tmp, "%d", &junk); 
         }
    }
    fclose(tmp); 
    
    out = fopen("networks.out", "w");  // clear contents of output file. 
    fclose(out); 
         
    in = fopen("networks.in", "r"); 
    if (in == NULL) {
       fprintf(stderr, "Coulnd't find input file. Program Terminating. \n"); 
       system("pause");
       exit(1); 
    }
    
    for (j = 0; j < iCases; j++) {  
         int totalMessages; // get from file (N)
         int totalPackets; // get from file (M)

         fscanf(in, "%d", &totalMessages); 
         fscanf(in, "%d", &totalPackets);

         int *size, *status; 
         size = (int*)malloc(sizeof(int) * totalMessages); 
         status = (int*)malloc(sizeof(int) * totalMessages); // holds where we reached for each message (in terms of bytes -- ie how many bytes for this message have already passed the buffer) 

         for (i = 0; i < totalMessages; i++) {
             fscanf(in, "%d", &size[i]); 
             status[i] = 0; 
         }

         packet *packets; 
         packets = (packet*)malloc(sizeof(packet)*totalPackets); 
         packet_init(packets, totalPackets); 

         for (i = 0; (i < totalPackets) && !feof(in); i++) {
             fscanf(in, "%d", &packets[i].ID); 
             fscanf(in, "%d", &packets[i].start); 
             fscanf(in, "%d", &packets[i].end); 
         }

         int buffersize = 0; 
         int maxsize = 0; 
         int lastDone = 0; // unused (for now?) 
         /*bonus points*/
         printf("Processing Case: %d\n\n", j+1); 
         /*end bonus points*/
         for (i = 0; i < totalPackets; i++) {
             if ((packets[i].done == 0) && (status[packets[i].ID-1] == packets[i].start-1)) { // packet should now pass the buffer. 
                 status[packets[i].ID-1] = packets[i].end; 
                 packets[i].done = 1; // true --> packet passes the buffer. 
                 /* bonus points*/ 
                 printf("Packet #%d will now pass the buffer. Its contents are: \t\nmsgID: %d\tstart: %d\tend: %d\n", i+1, packets[i].ID, packets[i].start, packets[i].end);
                 if (status[packets[i].ID-1] == size[packets[i].ID-1])
                    printf("Message %d has now completely passed the buffer. \n", packets[i].ID);
                 printf("Status for each message is now: \n"); 
                 int n; 
                 for (n = 0; n < totalMessages; n++)
                    printf("msg #%d -- passed: %d bytes out of: %d bytes. \n", n+1, status[n], size[n]); 
                 printf("\n"); 
                 /* end bonus points*/ 
                 i = -1; // use lastDone (instead of zero) for optimizations. needs some whiles, etc, not a big deal. 
                 buffersize = 0; // if we use lastDone, this will be wrong, we would have to update it accordingly. 
                 continue; 
             }
             else if (packets[i].done == 0){ // if packet hasn't passed the buffer, and shouldn't either --> queue it up. 
                 buffersize += (packets[i].end - packets[i].start +1); 
                 if (buffersize > maxsize)
                     maxsize = buffersize; 
             }
             // else the packet already passed the buffer. 
         }
         /*bonus points*/
         printf("End of case %d", j+1); 
         system("pause"); 
         system("cls"); 
         /*end bonus points*/

         free(packets); 
         free(size); 
         free(status); 
         
         out = fopen("networks.out", "a"); 
         fprintf(out, "Case 1: %d\n\n", maxsize); 
         fclose(out); 
    }
    
    fclose(in); 
    
    //system("pause"); 
    return 0; 
} 

void packet_init(packet list[], int max) {
     int i; 
     for (i = 0; i < max; i++) {
         list[i].ID = list[i].start = list[i].end = -1; // this line is useless. 
         list[i].done = 0; 
     }
}
void set(packet* pak, int msgID, int byteStart, int byteEnd) { // this function is unused. 
     pak->ID = msgID; 
     pak->start = byteStart; 
     pak->end = byteEnd; 
}
